<template>
  <div>
    <el-container style="background: #f5f7f9;">
      <el-main style="margin-top: 15px; background: #fff;padding: 12px 16px 0 16px;height: 100%;">
        <div style="height: 100%;margin-bottom: 12px;display: flex;">
          <el-button v-has="30401" size="small" type="primary" @click="add">新增</el-button>
        </div>
        <el-table ref="multipleTable" :data="table_body" stripe border :max-height="tableHeight">
          <el-table-column prop="name" label="名称" />
          <el-table-column prop="stageName" label="提单节点" />
          <el-table-column prop="createdAt" label="创建时间" />
          <el-table-column prop="status" label="状态" width="80">
            <template slot-scope="scope">
              <div v-if="scope.row.status===1"> 启用</div>
              <div v-else> 停用</div>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button v-has="30402" size="small" shape="circle" type="text" @click="edit(scope.row)">编辑</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-main>
      <el-footer style="padding:0;background: #fff;">
        <div style="text-align: right; padding-top: 10px;">
          <el-pagination background :current-page="queryData.currentPage" :page-sizes="[50, 100, 200, 300, 400]" :page-size="queryData.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="pageTotal" style="float: right;" @size-change="onPageSizeChange" @current-change="onPageChange" />
        </div>
      </el-footer>
    </el-container>
    <el-dialog :title="drawerTitle" :visible.sync="openDrawer" width="50%" :close-on-click-modal="false" top>
      <el-form>
        <el-row :gutter="32">
          <el-col :span="12">
            <el-form-item label="名称" label-position="top" required>
              <el-input v-model="formData.name" placeholder="名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="节点(请选择提单节点,可以多选或者不选)">
              <el-select v-model="formData.stageId" multiple class="width-inline" style="width:100%;">
                <el-option v-for="(d) in stageOptions" :key="d.id" :label="d.name" :value="d.id" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="类型(可以不选择)">
              <el-select v-model.lazy="formData.type" clearable class="width-inline" placeholder="选择类型" style="width:100%;">
                <el-option v-for="(d,i) in types" :key="i" :label="d.name" :value="d.id" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" style="margin-right: 8px" @click="openDrawer = false">取消</el-button>
        <el-button size="small" type="primary" @click="submit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import request from '@/utils/request'
import { copyProperty } from '@/utils/tools'
export default {
  data() {
    return {
      openDrawer: false,
      table_body: [

      ],
      styles: {
        height: 'calc(100% - 55px)',
        overflow: 'auto',
        paddingBottom: '53px',
        position: 'static'
      },
      formDataDefault: {
        id: '',
        name: '',
        status: 1,
        enable: 'true',
        stageId: [],
        type: ''
      },
      formData: {
        id: '',
        name: '',
        status: 1,
        enable: 'true',
        stageId: [],
        type: ''
      },
      queryData: {
        pageSize: 50,
        currentPage: 1,
        enable: 'true'
      },
      pageTotal: 0,
      pageSizeOpts: [50, 100],
      drawerTitle: '新增',
      stageOptions: [],
      types: [
          { id: 'AIR', name: '空运' },
          { id: 'SEA', name: '海运' }
          // { id: 'GND', name: '铁运' }
      ]
    }
  },
  computed: {
    tableHeight() {
      return document.documentElement.clientHeight - 250
    },
    pageSize() {
      return this.pageSizeOpts[0]
    }
  },
  created() {
    this.list()
    this.loadStageOption()
  },
  mounted() {
  },
  methods: {
    list() {
      request({
        url: 'batch/service-type/query?pageNum=' + this.queryData.currentPage + '&pageSize=' + this.queryData.pageSize,
        method: 'get'
      }).then(res => {
        if (res.success) {
          if (res.data) {
            this.table_body = res.data.records
            this.pageTotal = res.data.total
          }
        }
      })
    },
    loadStageOption() {
      request({
        url: 'batch/stage/option',
        method: 'get'
      }).then(res => {
        if (res.success) {
          this.stageOptions = res.data
        }
      })
    },
    submit() {
      if (this.formData.enable === 'true') {
        this.formData.status = 1
      } else {
        this.formData.status = 0
      }
      request({
        url: 'batch/service-type/add',
        method: 'post',
        data: this.formData
      }).then(res => {
        if (res.success) {
          this.openDrawer = false
          this.list()
        }
      })
    },
    edit(row) {
      this.drawerTitle = '编辑'
      copyProperty(this.formData, row)
      if (row.status === 1) {
        this.formData.enable = 'true'
      } else {
        this.formData.status = 'false'
      }
      this.openDrawer = true
    },
    add() {
      copyProperty(this.formData, this.formDataDefault)
      this.openDrawer = true
    },
    openDefDrawer() {
      this.openChargeDefDrawer = true
    },
    closeDefDrawer() {
      this.openChargeDefDrawer = false
    },
    onPageSizeChange(pageSize) {
      this.queryData.pageSize = pageSize
      this.list()
    },
    onPageChange(page) {
      this.queryData.currentPage = page
      this.list()
    }
  }
}
</script>
<style>
    .demo-drawer-footer{
        width: 100%;
        position: absolute;
        bottom: 0;
        left: 0;
        border-top: 1px solid #e8e8e8;
        padding: 10px 16px;
        text-align: right;
        background: #fff;
    }

    .demo-upload-list{
        display: inline-block;
        width: 60px;
        height: 60px;
        text-align: center;
        line-height: 60px;
        border: 1px solid transparent;
        border-radius: 4px;
        overflow: hidden;
        background: #fff;
        position: relative;
        box-shadow: 0 1px 1px rgba(0,0,0,.2);
        margin-right: 4px;
    }
    .demo-upload-list img{
        width: 100%;
        height: 100%;
    }
    .demo-upload-list-cover{
        display: none;
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        background: rgba(0,0,0,.6);
    }
    .demo-upload-list:hover .demo-upload-list-cover{
        display: block;
    }
    .demo-upload-list-cover i{
        color: #fff;
        font-size: 20px;
        cursor: pointer;
        margin: 0 2px;
    }
</style>
